//
//  ResponseViewController.m
//  MyBlog
//
//  Created by NCS on 16/12/22.
//  Copyright © 2016年 YSL. All rights reserved.
//

#import "ResponseViewController.h"

@interface ResponseViewController () {
    BOOL isExpanded;
    NSInteger fromWhere;
}
@end

@implementation ResponseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    isExpanded = NO;
    
    
//    self.contentField.delegate = self;
//    // hint
//    self.contentField.textColor = [UIColor lightGrayColor];
//    self.contentField.text = @"Please input your response...";
    
    [self.contentField becomeFirstResponder];
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    fromWhere = 1;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)cancel:(UIBarButtonItem *)sender {
    [self.contentField endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Click Cancel button to close modal view");
    }];
}

- (IBAction)send:(UIBarButtonItem *)sender {
    [self.contentField endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"Click Send button to close modal view");
    }];
}

- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    if (isExpanded) {
        self.bottomSpaceConstraint.constant = -271.0;
    } else {
        self.bottomSpaceConstraint.constant = 0.0;
    }
    
}

- (void)keyboardWillShow:(NSNotification *)aNotification {
    //获取键盘弹出前的Rect
    NSValue *keyBoardBeginBounds = [[aNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue *keyBoardEndBounds = [[aNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect = [keyBoardEndBounds CGRectValue];
    
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY = endRect.origin.y - beginRect.origin.y;
    NSLog(@"Show keyboard - Y : %f",deltaY);
    
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    [UIView animateWithDuration:0.25f animations:^{
        [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y + deltaY, self.bottomView.frame.size.width, self.bottomView.frame.size.height)];
    }];
    isExpanded = YES;
    [self updateViewConstraints];

    
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification {
    
    //获取键盘弹出前的Rect
    NSValue *keyBoardBeginBounds=[[aNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect beginRect=[keyBoardBeginBounds CGRectValue];
    
    //获取键盘弹出后的Rect
    NSValue *keyBoardEndBounds=[[aNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect  endRect=[keyBoardEndBounds CGRectValue];
    
    //获取键盘位置变化前后纵坐标Y的变化值
    CGFloat deltaY = beginRect.origin.y - endRect.origin.y;
    NSLog(@"Hide keyboard - Y : %f",deltaY);
    
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    [UIView animateWithDuration:0.25f animations:^{
        [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y - deltaY, self.bottomView.frame.size.width, self.bottomView.frame.size.height)];
    }];
    isExpanded = NO;
    [self updateViewConstraints];
}

- (void)checkWhereFrom:(NSInteger)from {
    if (from == 1l) {
        
    }
}

- (IBAction)checkCommentOrForward:(UIButton *)sender {
    
}

//-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
//
//    self.contentField.textColor = [UIColor blackColor];
//    self.contentField.text=@"";
//
// 
//    return YES;
//}
//
//- (void)textViewDidChange:(UITextView *)textView {
//    if ([self.contentField.text isEqualToString:@""]) {
//        self.contentField.textColor = [UIColor lightGrayColor];
//        self.contentField.text = @"Please input your response...";
//    }
////    else {
////        self.contentField.textColor = [UIColor blackColor];
////        self.contentField.text=@"";
////    }
//}

@end
